package com.home.free.controller.user;

import com.home.free.controller.BaseController;
import com.home.free.entity.UserStatusEnum;
import com.home.free.service.UserService;
import com.home.free.util.DateUtil;
import com.home.free.util.PageRet;
import com.home.free.util.Ret;
import com.home.free.vo.user.UserQuery;
import com.home.free.vo.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Title: com.home.free.controller.user<br>
 * Description: <br>
 * Copyright: Copyright (c) 2015<br>
 * Company: 北京云杉世界信息技术有限公司<br>
 *
 * @author gaotianjie
 * @date 2019/2/20
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(Model model) {
        model.addAttribute("name", "世界你好");
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", "世界你好");

        List<Map<String, Object>> userStatusMap =  UserStatusEnum.getList();
        mav.addObject("time", DateUtil.nowToString());
        mav.setViewName("/user/index");
        return mav;
    }

    @RequestMapping(value = "/getAllUserList")
    @ResponseBody
    public PageRet<List<UserVo>> getAllUserList(UserQuery userQuery) {

        Integer count = userService.pageUserCount(userQuery);
        if (count > 0) {
            List<UserVo> userVoList = userService.pageUserList(userQuery);

            return PageRet.retTure(userVoList, count);
        }

        return PageRet.retTure();
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public Ret<Integer> addUser(UserVo userVo) {
        Integer count = userService.saveUser(userVo);
        return Ret.retTrue(count);
    }
}
